﻿/*
    基金管理员部门组
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace BenefitHelper.Sys
{
    /// <summary>
    /// 用户组表(需要)
    /// </summary>
    public class Groups
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 小组名
        /// </summary>
        [Display(Name = "小组名称")]
        public string GroupName { get; set; }
        /// <summary>
        /// 父级小组编号
        /// </summary>
        [Display(Name = "父小组名称")]
        public int ParentGroupId { get; set; }
        /// <summary>
        /// 是否有子小组
        /// </summary>
        public bool HasChildren { get; set; }
        /// <summary>
        /// 是否已被删除
        /// </summary>
        public bool IsDeleted { get; set; }

        /// <summary>
        /// 增加一个小组，默认没有子小组
        /// </summary>
        /// <returns></returns>
        public int AddGroup(DB.DBManager db)
        {
            Groups g = new Groups();
            g.GroupName = this.GroupName;
            g.HasChildren = false;
            g.ParentGroupId = this.ParentGroupId;
            g.IsDeleted = false;
            var query = db.Groups.Where(a => a.Id == g.ParentGroupId);
            if (query.Count() > 0)
            {
                Groups group = query.First();
                group.HasChildren = true;
            }
            db.Groups.Add(g);
            db.SaveChanges();
            return g.Id;
        }

        public List<Groups> GetGroups(DB.DBManager db)
        {
            var query = db.Groups.Where(a => a.IsDeleted == false).ToList();
            return query;
        }

        /// <summary>
        /// 得到交易员可以看到的小组的列表
        /// </summary>
        /// <param name="operatorid"></param>
        /// <returns></returns>
        public List<Groups> GetOperatorCouldSeeGroups(int operatorid)
        {
            List<Groups> groups = new List<Groups>();

            return groups;
        }

        public Groups GetGroup(int id, DB.DBManager db)
        {
            return db.Groups.Find(id);
        }
    }
}